package unsw.dao;

import unsw.model.FileStatus;
import unsw.model.SatelliteFile;
import unsw.utils.DatabaseConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SatelliteFileDao {
    private static final Connection conn;

    static {
        try {
            conn = DatabaseConnection.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


    public void addFileToSatellite(String satelliteId, String filename, String content, String receivedData) {
        SatelliteFile fileByFileName = getFileByFileName(filename, satelliteId);
        int fileSize = content.length();
        if (fileByFileName == null) {
            //如果文件不存在,则新增文件
            String sql = "INSERT INTO satellitefile ( filename, data,fileSize,satelliteId,status,transferreddata) VALUES (?, ?, ?, ?, ?, ?)";
            try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                pstmt.setString(1, filename);
                pstmt.setString(2, content);
                pstmt.setInt(3, fileSize);
                pstmt.setString(4, satelliteId);
                pstmt.setString(5, FileStatus.TransferringToSatellite.name());
                pstmt.setString(6, receivedData);
                pstmt.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }

    public void updateFileTransferreddata(String fileName, String satelliteId, String readyData) {
        String sql = "UPDATE satellitefile SET transferreddata=concat(transferreddata,?) WHERE fileName = ? and satelliteId = ?";
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, readyData);
            pstmt.setString(2, fileName);
            pstmt.setString(3, satelliteId);
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public SatelliteFile getFileByFileName(String fileName, String satelliteId) {
        String sql = "SELECT id, filename, data, fileSize,status,transferreddata FROM satellitefile WHERE filename = ? and satelliteId = ?";
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, fileName);
            pstmt.setString(2, satelliteId);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    int id = rs.getInt("id");
                    String data = rs.getString("data");
                    int fileSize = rs.getInt("fileSize");
                    String status = rs.getString("status");
                    String transferreddata = rs.getString("transferreddata");
                    return new SatelliteFile(id, fileName, data, fileSize, satelliteId, status, transferreddata);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void updateFileStatus(String fileName, String satelliteId, FileStatus status) {
        String sql = "UPDATE satellitefile SET status= ? WHERE fileName = ? and satelliteId = ?";
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, status.name());
            pstmt.setString(2, fileName);
            pstmt.setString(3, satelliteId);
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void deleteFileBySatelliteId(String satelliteId) {
        String sql = "DELETE FROM satellitefile WHERE satelliteId = ? ";
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, satelliteId);
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void resetSatelliteFileStatus(SatelliteFile file, String fromId) {
        String sql = "UPDATE satellitefile SET status= ? WHERE filename = ? and satelliteId = ?";
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, FileStatus.TransferToSatelliteCompleted.name());
            pstmt.setString(2, file.getFilename());
            pstmt.setString(3, fromId);
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
